// JavaScript Document
function changeGT(){
	var go_p=$("#go").val();
	var to_p=$("#to").val();	
	var temp=go_p;
	$("#go").val(to_p);
	$("#to").val(temp);
}

//预定票
function ydTickets(i){
	var val=$($(i).parent()).find("input[type=hidden]").val();
	var info=val.split("_");
	var tid=0;
	var pid=0;
	if(info[0]=="tt"){
		tid=info[1];
	}else{
		pid=info[1];
	}
	$.ajax({
	    url: "../json/TravelTicket_getTicketInfo",    //请求的url地址
	    dataType: "json",   //返回格式为json
	    async: true, //请求是否异步，默认为异步，这也是ajax重要特性
	    type: "POST",
	    data:{"tt.tid":tid,"pt.pid":pid},
	    success: function(data) {
	    	if(data.result>0){
	    		openWindow();
	    	}else{
	    		alert("出错了!");
	    	}
	    }
	});
}

function openWindow() {
	var url="win.html";
	var w_sp1 = 400;
	var h_sp1 = 250;
	var iTop2 = (window.screen.availHeight - 20 - h_sp1) / 2;
	var iLeft2 = (window.screen.availWidth - 10 - w_sp1) / 2;
	var params = 'menubar:no;dialogHeight=' + h_sp1 + 'px;dialogWidth=' + w_sp1 + 'px;dialogLeft=';
	params +=iLeft2 + 'px;dialogTop=' + iTop2 + 'px;resizable=yes;scrollbars=0;resizeable=0;center=yes;location:no;status:no';
	var v = window.showModalDialog(url, window, params);
	//alert(v);  //v是窗口返回值
}

//搜索列车信息
function searchTrain(){
	var total=0;
	var tgo=$("#go").val();
	var tto=$("#to").val();
	var t=$("#mydate").val();
	var d=t.split("-")[1]+"月"+t.split("-")[2]+"日";
	$("#go_place").text(tgo);
	$("#to_place").text(tto);
	$(".d_r").text(d);
	$(".tic_result").css("display","block");
	var info='<tr style="font-size:12px;color:#666;height:50px;background:#ccc;"><td>车次&nbsp;/&nbsp;班次</td><td>发时';
	info+='&nbsp;/&nbsp;到时</td><td>始发站&nbsp;/&nbsp;终点站</td><td>运行时间</td><td>余票</td><td>参考票价</td><td></td></tr>';
	info+='<tr style="height:10px;width:100%;"></tr><tr style="height:50px;text-align:left;"><td style="border-bottom:1px dashed #ccc;">>>&nbsp;火车</td></tr>';
	$.ajax({
	    url: "../json/TravelTicket_searchTrainTickets",    //请求的url地址
	    dataType: "json",   //返回格式为json
	    async: true, //请求是否异步，默认为异步，这也是ajax重要特性
	    type: "POST",
	    data:{"tt.tgo":tgo,"tt.tto":tto,"tt.tdate":t},
	    success: function(data) {
	    	if(data.total != 0){
	    		total+=data.total;
				$.each(data.rows,function(index,item){
					info+='<tr style="height:10px;width:100%;"></tr><tr style="border:1px solid #ccc;margin:10px 0;">';
		            info+='<td style="font-size:20px;">'+item.tname+'</td><td style="line-height:30px;">'+item.ctime+'<br>'+item.etime+'</td>';
		            info+='<td style="font-size:14px;line-height:30px;">'+item.tgo+'<br>'+item.tto+'</td><td style="font-size:14px;color:#333;">'+item.runtime+'</td>';
		            info+='<td style="font-size:14px;line-height:25px;">'+item.tprice.split("_")[0]+'<br>'+item.tprice.split("_")[1]+'<br>'+item.tprice.split("_")[2]+'</td><td>';
		            info+='<input class="yd_btn" type="button" onclick="ydTickets(this);" value="预定"><input type="hidden" value="tt_'+item.tid+'"></td></tr>';
				});
			}else{
				info+='<tr style="font-size:12px;width:100%;height:50px;"><td> </td><td> </td><td>暂无数据</td></tr>'; 
			}
			info+='<tr style="height:10px;width:100%;"></tr><tr style="height:50px;text-align:left;"><td style="border-bottom:1px dashed #ccc;">>>&nbsp;飞机</td></tr><tr style="height:10px;width:100%;"></tr>';
			$("#tickets_info").html(info);
			plane();
	    }
	});
	
	function plane(){
		$.ajax({
		    url:"../json/TravelTicket_searchPlaneTickets",   //请求的url地址
		    dataType: "json",   //返回格式为json
		    async: true, //请求是否异步，默认为异步，这也是ajax重要特性
		    type: "POST",
		    data:{"pt.pgo":tgo,"pt.pto":tto,"pt.pdate":t},
		    success: function(data) {
		    	if(data.total != 0){
		    		total+=data.total;
					$.each(data.rows,function(index,item){
						info+='<tr style="height:10px;width:100%;"></tr><tr style="border:1px solid #ccc;margin:10px 0;">';
			            info+='<td style="font-size:20px;">'+item.pname+'</td><td style="line-height:30px;">'+item.ctime+'<br>'+item.etime+'</td>';
			            info+='<td style="font-size:14px;line-height:30px;">'+item.pgo+'<br>'+item.pto+'</td><td style="font-size:14px;color:#333;">'+item.runtime+'</td>';
			            info+='<td style="font-size:14px;line-height:25px;">￥'+item.pprice+'起</td><td><input class="yd_btn" type="button" onclick="ydTickets(this);" value="预定"><input type="hidden" value="pt_'+item.pid+'"></td></tr>';
					});
				}else{
					info+='<tr style="font-size:12px;width:100%;height:50px;"><td> </td><td> </td><td>暂无数据</td></tr>'; 
				}
				info+='<tr style="height:10px;width:100%;"></tr>';
				$("#tickets_info").html(info);
				$("#ticket_result").text(total);
		    }
		});
	}
}